package com.controller;

import com.model.user.User;
import com.service.user.UserService;
import com.thread.AccessTokenThread;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2016/11/12.
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    private static Logger log = LoggerFactory.getLogger(UserController.class);


    //测试
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {
        String a1=JSONObject.fromObject("{\"group\":{\"name\":\"\"}}").toString();
        String a = JSONObject.fromObject(getGroup()).toString();
        log.info(a1 + getGroup().toString());
        return a;
    }

    //创建分组
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create() {
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        String b = userService.createGroup(getGroup(), at);
        String j = JSONObject.fromObject(getGroup()).toString();
        log.info(j);
        return b;
    }

    //查询所有分组
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String get() {
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        String b = userService.getAllGroups(at);
        return b;
    }


    //查询用户所在分组
    @RequestMapping(value = "/getone",method = RequestMethod.POST)
    public String getone(){
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        String b=userService.getone(getoneuser(),at);
        return b;
    }

    //修改分组名
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update() {
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        String b = userService.updateGroup(updateGroup(), at);
        String j = JSONObject.fromObject(getGroup()).toString();
        log.info(j);
        return b;
    }

    //移动用户分组
    @RequestMapping(value = "/move",method = RequestMethod.POST)
    public String move(){
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        String b = userService.moveUser(moveuser(), at);
        String j = JSONObject.fromObject(getGroup()).toString();
        log.info(j);
        return b;
    }

    //批量移动用户分组
    //暂时不做。

    //删除分组
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public void delete(){
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        JSONObject jsonObject=userService.deleteGroup(deleteGroup(),at);
        int result=jsonObject.getInt("errcode");
        if(result==0){
            log.info("删除成功");
        }else {
            log.error("删除失败，errcode="+String.valueOf(result));
        }
    }

    //创建名为test的组
    public String  getGroup() {
        User user = new User();
        user.setName("kashjdsak");
        String a1=JSONObject.fromObject("{\"group\":{\"name\":\""+user.getName()+"\"}}").toString();
        return a1;
    }
    //查询唯一的用户在哪一组
    public User  getoneuser() {
        User user = new User();
        user.setOpenid("obcuqxGCSU44QIdKCu-vyPFJ5Ssc");
        return user;
    }
    //修改名为test的组为test2
    public String  updateGroup() {
        User user = new User();
        user.setId("100");
        user.setName("test2");
        String a1=JSONObject.fromObject("{\"group\":{\"id\":"+user.getId()+",\"name\":\""+user.getName()+"\"}}").toString();
        return a1;
    }

    //移动用户
    public User  moveuser() {
        User user = new User();
        user.setOpenid("obcuqxGCSU44QIdKCu-vyPFJ5Ssc");
        user.setTo_groupid("100");
        return user;
    }

    //删除id为100的组
    public String  deleteGroup() {
        User user = new User();
        user.setId("100");
        user.setName("test2");
        String a1=JSONObject.fromObject("{\"group\":{\"id\":"+user.getId()+"}}").toString();
        return a1;
    }
}
